package we.libQ 
{
	import flash.display.BitmapData;
	/**
	 * ...
	 * @author worldedit
	 */
	public class BlitGeom 
	{
		private var target:BitmapData;
		
		public function BlitGeom() 
		{
			
		}
		
		public function setTarget(target:BitmapData):void
		{
			this.target = target;
		}
		
		/**
﻿		 * Renders a quick line to the target
﻿		 * @param x X coordinate of the first endpoint
﻿		 * @param y Y coordinate of the second endpoint
﻿		 * @param x2 X coordinate of the first endpoint
﻿		 * @param y2 Y coordinate of the second endpoint
﻿		 * @param color Color value of the line to be drawn
﻿		 */
﻿  ﻿  	public function fastLine(x:int,y:int,x2:int,y2:int,color:uint):void
﻿  ﻿  	{
			var shortLen:int = y2 - y;
			var longLen:int = x2 - x;
﻿			if (! longLen)
﻿			{
﻿				if (! shortLen)
﻿		  		{
﻿		  ﻿  	return;
﻿		  		}
﻿			}
﻿			var i:int,id:int,inc:int;
﻿			var multDiff:Number;
		
﻿			target.lock();
		
﻿			if((shortLen ^ (shortLen >> 31)) -(shortLen >> 31) > (longLen ^ (longLen >> 31)) -(longLen >> 31))
﻿			{
﻿				if (shortLen < 0)
﻿				{
﻿					inc = -1;
﻿					id =  -  shortLen % 4;
﻿				}
﻿				else
﻿				{
﻿					inc = 1;
﻿			  		id = shortLen % 4;
﻿	  ﻿  		}
﻿	  ﻿  		multDiff = ! shortLen ? longLen:longLen / shortLen;
	
﻿				if (id)
﻿				{
					target.setPixel32(x,y,color);
﻿					i += inc;
﻿				if (--id)
﻿				{
					target.setPixel32(x+i*multDiff,y+i,color);
﻿					i+=inc;
﻿					if (--id)
﻿					{
						target.setPixel32(x+i*multDiff,y+i,color);
﻿			  ﻿  		i+=inc;
﻿					}
﻿				}
﻿	  ﻿  	}

﻿	  ﻿  	while (i!=shortLen)
﻿	  ﻿  	{
				target.setPixel32(x+i*multDiff,y+i,color);
﻿				i += inc;
﻿				target.setPixel32(x+i*multDiff,y+i,color);
﻿				i += inc;
﻿				target.setPixel32(x+i*multDiff,y+i,color);
﻿				i += inc;
﻿				target.setPixel32(x+i*multDiff,y+i,color);
﻿				i+=inc;
				}
﻿			}
﻿			else
﻿			{
﻿				if (longLen<0)
﻿				{
﻿					inc=-1;
﻿					id=- longLen%4;
﻿				}
﻿				else
﻿				{
﻿					inc=1;
﻿					id=longLen%4;
﻿	  ﻿  		}
﻿	  ﻿  		multDiff=! longLen?shortLen:shortLen/longLen;
	
﻿	  ﻿  		if (id)
﻿				{
﻿					target.setPixel32(x,y,color);
﻿					i+=inc;
﻿					if (--id)
﻿					{
						target.setPixel32(x+i,y+i*multDiff,color);
﻿						i+=inc;
﻿						if (--id)
﻿						{ 
							target.setPixel32(x+i,y+i*multDiff,color);
﻿			  ﻿  			i+=inc;
﻿						}
﻿					}
﻿	  ﻿  		}
﻿	  ﻿  		while (i!=longLen)
﻿	  ﻿  		{
					target.setPixel32(x+i,y+i*multDiff,color);
	﻿	  ﻿  ﻿		i+=inc;
					target.setPixel32(x+i,y+i*multDiff,color);
	﻿	  ﻿  ﻿		i+=inc;
					target.setPixel32(x+i,y+i*multDiff,color);
	﻿	  ﻿  ﻿		i+=inc;
					target.setPixel32(x + i, y + i * multDiff, color);
	﻿	  ﻿  ﻿		i+=inc;
﻿	  ﻿  		}
﻿  			}
			target.unlock();
﻿  ﻿  	}

		public function fastCircle(x0:int, y0:int, r:int, c:int):void
		{
			var f:int = 1 - r;
			var ddF_x:int = 1;
			var ddF_y:int = -2 * r;
			var x:int = 0;
			var y:int = r;
			target.setPixel32(x0, y0 + r,c);
			target.setPixel32(x0, y0 - r,c);
			target.setPixel32(x0 + r, y0,c);
			target.setPixel32(x0 - r, y0,c);
			while(x < y)
			{
				if(f >= 0) 
				{
					y--;
					ddF_y += 2;
					f += ddF_y;
				}
				x++;
				ddF_x += 2;
				f += ddF_x;    
				target.setPixel32(x0 + x, y0 + y,c);
				target.setPixel32(x0 - x, y0 + y,c);
				target.setPixel32(x0 + x, y0 - y,c);
				target.setPixel32(x0 - x, y0 - y,c);
				target.setPixel32(x0 + y, y0 + x,c);
				target.setPixel32(x0 - y, y0 + x,c);
				target.setPixel32(x0 + y, y0 - x,c);
				target.setPixel32(x0 - y, y0 - x,c);
			}
		}
	}

}